<?php 
session_start();

$returnUrl = "";
if (array_key_exists("returnUrl", $_REQUEST))
{
        $returnUrl = $_REQUEST['returnUrl'];
}
if($returnUrl=="")
        $returnUrl      = "index.php";

if ($_SESSION && array_key_exists('idUser', $_SESSION) )
{
        header("Location: ".$returnUrl);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="JavaScript desktop environment built with jQuery." />
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE" />
<title>Studio Fol&igrave;</title>
<script type="text/javascript" src="resources/Javascript/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="resources/Javascript/Main.js"></script>
<script type="text/javascript" src="resources/Javascript/FileSystem.js"></script>
<link rel="stylesheet" href="resources/Css/login.css" />
<?php 
$username = "";

if (array_key_exists("user", $_SESSION) && $_SESSION['user'] != '')
{		
?>
	<script type="text/javascript">
		document.location.href='index.php';
    </script>
<?php 
}
?>
</head>
<body class="login">

    <div id="login">

        <h1>
            <a title="eSurv" href="#">Studio Fol&igrave;</a>
        </h1>



        <form action="<?=$returnUrl?>" onsubmit="return false;" method="post" id="formLogin" name="formLogin">
            <input type="hidden" id="fn" name="fn" value="login"/>

            <input type="hidden" id="value" name="value" value="0"/>
            <input type="hidden" id="msgRet" name="msgRet" value=""/>
            <input type="hidden" id="changePw" name="changePw" value="0"/>

                <div class="MessageBox" id="MessageBox"></div>
                <br><br>

                <div id="fieldForm">

                    <div clasS="field">
                        <label class="usernamelabel" for="username">User Name</label>
                        <br>
                        <input type="text" id="username" name="username" autocomplete=off>
                    </div>
                    <br />
                    <div clasS="field">
                        <label class="passwordlabel" for="password">Password</label>
                        <br>
                        <input type="password" id="password" name="password" onkeypress="" autocomplete=off>
                    </div>
                    <br />
                    <br />


                    <p class="submit">

                       <button type="submit" name="wp-submit" id="submit-go" class="button-primary">Entra</button>

                    <p>
                </div>

        </form>
        
        
        <p id="backtoblog"><a href="#" title="WebBox">&rarr; WebBox - Login page</a></p>


    </div>

    <script type="text/javascript">
    $(document).ready(function() {

        function login()
        {
            if($("#username").val()==""){

                $(".usernamelabel").css('color', 'red');
                $("#username").css('border-color', 'red');
                $("#username").focus();
                
                return;
                
            } else if ($("#password").val()=="") {

                $(".passwordlabel").css('color', 'red');
                $("#password").css('border-color', 'red');
                $("#password").focus();
                return;

            } else {
				var str = $("#formLogin").serialize();
                $.post(
                    "/resources/WebServices/Login.php",
                    str,
                    function(data) {

                        var ret = eval(data)[0];
                        var Username = ret.Username;
                        var idUser = ret.idUser;
                        var msg = ret.msg;
                        var redirect = ret.redirect;
                        
                        if (Username == '' || Username == null)
                        {

                            /*
                            *Show message KO
                            */
                            $('#MessageBox').removeClass('MessageBox');
                            $('#MessageBox').addClass('MessageBoxError');

                            $('#MessageBox').html(msg);
                            $('#MessageBox').css('display', 'block');

                            
                            return;

                        } else  if (Username != '') {
                            
                        	$('#MessageBox').removeClass('MessageBoxError');
                            $('#MessageBox').addClass('MessageBox');
                            $('#MessageBox').html('accesso al sistema in corso...');
                            $('#MessageBox').css('display', 'block');

                            $("#value").val(idUser);

                            $("#msgRet").val(msg);

                            document.location.href = redirect;

                        }


                });
            }
        }

       $("#submit-go").click(function() {
           login();
       });

        $("#username").focus();

    });



    

    </script>
</body>
</html>